﻿#region Using

using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using chatblog;
using RuleCode.Table;

#endregion

namespace map.member
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridBind();
        }

        private void GridBind()
        {
            string userid = AliApi.appUserId;
            cbfriendreference shop = cbfriendreference.Find("cbuserid = '" + userid + "'");
            GridView1.DataSource = shop.MyTable;
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            cbfriendreference uc = cbfriendreference.Find(" recid = '" + id + "'", "RECID");
            uc.Delete();
            GridBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            cbfriendreference uc = cbfriendreference.Find(" recid = '" + id + "'", "RECID");

            TextBox2.Text = uc.Name.ValueString;
            TextURL.Text = uc.URL.ValueString;
            TextDesp.Text = uc.Description.ValueString;
            friendrefimage.ImageUrl = ResolveUrl(uc.ImagePath.ValueString);
            Label_recid.Text = uc.RECID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            cbfriendreference uc = cbfriendreference.Find(" 1=2 ");
            uc.New();

            uc.cbuserid.Value = AliApi.appUserId;
            uc.Name.Value = TextBox2.Text;
            uc.URL.Value = TextURL.Text;
            uc.Description = TextDesp.Text;
            uc.ImagePath = friendrefimage.ImageUrl;
            uc.Insert();
            GridBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            cbfriendreference uc = cbfriendreference.Find(" recid = '" + Label_recid.Text + "'", "RECID");
            uc.Name.Value = TextBox2.Text;
            uc.URL.Value = TextURL.Text;
            uc.Description = TextDesp.Text;
            uc.ImagePath = friendrefimage.ImageUrl;
            uc.Modify();
            GridBind();
        }

        protected void BTNUp_Click(object sender, EventArgs e)
        {
            showError.Text = "";
            bool fileOK = false;

            if (FileUpload1.HasFile)
            {
                //返回文件的扩展名
                String fileExtension = Path.GetExtension(FileUpload1.FileName).ToLower();
                //设置限制的文件类型
                String[] allowExtension = {".jpg", ".gif", ".bmp", "png"};
                //判断用户选择的文件类型是否受限制
                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        fileOK = true;
                    }
                }
                //大于1M的文件不能上传
                if (FileUpload1.PostedFile.ContentLength > 1024000)
                {
                    fileOK = false;
                }
            }
            string strImg2 = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(Util.UploadImageDirectory) + time2;
            string linkpth = ResolveUrl(Util.UploadImageDirectory) + time2;

            if (!fileOK)
            {
                showError.Text = "图片格式不正确对或者超过1M！";
            }
            if (fileOK)
            {
                FileUpload1.PostedFile.SaveAs(strFilePth);
                Response.Write("<script>alert('上传成功！');</script>");
                BizAreaImages images = BizAreaImages.Find(" 1=2 ");
                images.New();

                images.BizAreaImagePath = linkpth;

                images.Insert();
                friendrefimage.ImageUrl = linkpth;
            }
        }
    }
}